﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MaxLeaf.KH.Contract;
using MaxLeaf.KH.BLLImplementation;
using CommonsLib.Utility;
using MaxLeaf.KH.DataContract;
using CommonsLib.Settings;

namespace MaxLeaf.KH.BLLProxy
{
    public class BasicMtnBLLProxy : BaseBLLProxy,IExecute, IBasicMtn
    {
        public   IBasicMtn CurrentBLL
        {
            get;
            set;
        }
        
        public BasicMtnBLLProxy():base()
        {
            switch ( Access_Mode )
            {
                
                case MaxLeaf.Commons.Enum.ACCESS_Direct:
                    {
                        CurrentBLL = new BasicMtnBLL();
                        break;
                    }

                case MaxLeaf.Commons.Enum.ACCESS_IIS:
                    {
                        CurrentBLL = new BasicMtnBLLWCFClient(this.WCF_Configs);
                        break;
                    }

                 
                default: throw new Exception(MaxLeaf.Commons.Const.EXCEPTION_MESSAGE_INVALID_ACCESSMODE_CONFIG);
            }
        }
        
        
        

        public override IDictionary<string, object> ExecuteCmd(string cmd, IDictionary<string, object> inputData)
        {
             

                Dictionary<string, object> outputData = new Dictionary<string, object>();

                object data = inputData["data"];

                if (cmd.Equals("Select"))
                {
                    data = this.Select(data, CvtFunc.ToInt(inputData["KeyValue"]));
                    outputData["data"] = data;
                }
                else if (cmd.Equals("SelectDtl"))
                {

                    outputData["data"] = this.SelectDtl(data, CvtFunc.ToInt(inputData["KeyValue"])); ;
                }
                else if (cmd.Equals("Add"))
                {
                    data = this.Add(data);
                    outputData["data"] = data;
                }
                else if (cmd.Equals("Update"))
                {
                    data = this.Update(data);
                    outputData["data"] = data;
                }
                else if (cmd.Equals("Delete"))
                {
                    data = this.Delete(data);
                    outputData["data"] = data;
                }
                else
                {
                    throw new Exception(string.Format(MaxLeaf.Commons.Const.EXCEPTION_MESSAGE_INVALID_COMMANDNAME, cmd));
                }



                return outputData;
             

        }


        public object Select(object model, int intKeyValue)
        {
            
                return CurrentBLL.Select(model, intKeyValue);
             
        }

        public List<object>  SelectDtl(object model, int intForeignKeyValue)
        {

            return CurrentBLL.SelectDtl(model, intForeignKeyValue);

        }

        public object Add(object model)
        {
            
            return CurrentBLL.Add(model);
                 
        }

        public object Update(object model)
        {
             
            return CurrentBLL.Update(model);
            
        }

        public object Delete(object model)
        {
             
            return CurrentBLL.Delete(model);
            
        }
    }
}
